package com.dang.alex.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONArray;
import org.json.JSONObject;

import com.dang.alex.model.CartItem;
import com.dang.alex.model.Order;
import com.dang.alex.model.ReceiveAddress;
import com.dang.alex.model.User;
import com.dang.alex.service.ReceiveAddressService;
import com.dang.alex.service.ServiceDAOFactory;

@WebServlet("/order.do")
public class OrderServlet extends HttpServlet{
	
	private static final long serialVersionUID = 5281685395163193621L;
	public static final String METHOD_CONFIRM = "0";
	public static final String METHOD_SUBMIT = "1";
	public static final String METHOD_ADDRESS = "2";
	public static final String METHOD_FINISH = "3";
	
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String method = req.getParameter("method");
		HttpSession session = req.getSession();
		Map<Integer, CartItem> map = (Map<Integer, CartItem>) session.getAttribute("s_car");
		if (method != null) {
			switch(method) {
			case OrderServlet.METHOD_CONFIRM:
				confirm(req, resp, map);
				break;
			case OrderServlet.METHOD_SUBMIT:
				submit(req, resp);
				break;
			case OrderServlet.METHOD_ADDRESS:
				address(req, resp);
				break;
			case OrderServlet.METHOD_FINISH:
				finish(req, resp);
				break;
			default:
				break;
			}
		}
	}
	
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req,resp);
	}
	
	private void finish(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		ReceiveAddress addr = new ReceiveAddress();
		Order order = new Order();
		HttpSession session = req.getSession();
		try {
			String receiveName = req.getParameter("receiveName");
			String fullAddress = req.getParameter("fullAddress");
			String postalCode = req.getParameter("postalCode");
			String phone = req.getParameter("phone");
			String mobile = req.getParameter("mobile");
			
			int userId = ((User)session.getAttribute("s_user")).getId();
			
			Map<Integer, CartItem> map = (Map<Integer, CartItem>) session.getAttribute("s_car");
			
			if(map.isEmpty()) {
				req.getRequestDispatcher("/WEB-INF/view/order/order_ok.jsp").forward(req, resp);
			}
			
			addr.setUserId(userId);
			addr.setFullAddress(fullAddress);
			addr.setReceiveName(receiveName);
			addr.setPhone(phone);
			addr.setPostalCode(postalCode);
			addr.setMobile(mobile);
			
			order = new Order();
			order.setFullAddress(addr.getFullAddress());
			order.setUserId(userId);
			order.setMobile(addr.getMobile());
			order.setOrderDesc("desc demo");
			order.setOrderTime(System.currentTimeMillis());
			order.setPhone(addr.getPhone());
			order.setPostalCode(addr.getPostalCode());
			order.setReceiveName(addr.getReceiveName());
			order.setStatus(0);
			order.setTotalPrice(total(map));
			
			ReceiveAddressService service = ServiceDAOFactory.getReceiveAddressService();
			if(service.findById(addr.getId()) == null) {
				service.sava(addr);
			}
			
			ServiceDAOFactory.getOrderService().sava(order);
			map.clear();
			session.setAttribute("s_car", map);
			
			req.setAttribute("orderId", order.getId());
			req.setAttribute("totalPrice", order.getTotalPrice());
			req.getRequestDispatcher("/WEB-INF/view/order/order_ok.jsp").forward(req, resp);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	private void submit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.getRequestDispatcher("/WEB-INF/view/order/address_form.jsp").forward(req, resp);
	}
	
	public double total(Map<Integer, CartItem> map) {
		double totalPrice = 0.0;
		for(Map.Entry<Integer, CartItem> entry: map.entrySet()) {
			totalPrice += entry.getValue().getNum() * entry.getValue().getPro().getDangPrice();
		}
		BigDecimal bd = new BigDecimal(totalPrice);
		BigDecimal bd1 = bd.setScale(2, bd.ROUND_HALF_UP);
		return bd1.doubleValue();
	}
	
	private void confirm(HttpServletRequest req, HttpServletResponse resp, Map<Integer, CartItem> map) throws ServletException, IOException {
		req.setAttribute("totalDang", total(map));
		req.setAttribute("s_car", map);
		req.getRequestDispatcher("/WEB-INF/view/order/order_info.jsp").forward(req, resp);
	}
	
	private void address(HttpServletRequest req, HttpServletResponse resp) {
		int userId = ((User)req.getSession().getAttribute("s_user")).getId();
		List<ReceiveAddress> addrs;
		try {
			addrs = ServiceDAOFactory.getReceiveAddressService().findByUserId(userId);
			JSONObject json = new JSONObject();
			JSONArray array = new JSONArray();
			for(ReceiveAddress receiveAddress : addrs) {
				JSONObject jsonDetail = new JSONObject();
				jsonDetail.put("id", receiveAddress.getId());
				jsonDetail.put("userId", receiveAddress.getUserId());
				jsonDetail.put("fullAddress", receiveAddress.getFullAddress());
				jsonDetail.put("mobile", receiveAddress.getMobile());
				jsonDetail.put("phone", receiveAddress.getPhone());
				jsonDetail.put("postalCode", receiveAddress.getPostalCode());
				array.put(jsonDetail);
			}
			json.put("addrs", array);
			PrintWriter out = resp.getWriter();
			out.print(json.toString());
			out.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
